package com.five.core.result;


import java.util.Map;

/**
 * Created by shelei on 2024/10/18
 */
public class Result <T>{

    /**
     * 响应的状态
     */
    private Integer code;

    /**
     * 提示消息
     */
    private String msg;

    /**
     * 总记录数
     */
    private Long count;

    /**
     * 响应的数据
     */
    private T data;


    /**
     * 构建分页的结果
     * @param count
     * @param data
     * @return
     */
    public static Result buildSuccess(Long count,Object data){
        Result result=new Result();
        result.setCode(ResultEnum.REQUEST_SUCCESS.getCode());
        result.setMsg(ResultEnum.REQUEST_SUCCESS.getMessage());
        result.setCount(count);
        result.setData(data);
        return result;
    }


    /**
     * 构建单个结果的成功
     * @param data
     * @return
     */
    public static Result buildSuccess(Object data){
       return buildSuccess(null,data);
    }

    /**
     * 构建不带数据的成功
     * @return
     */
    public static Result buildSuccess(){
        return buildSuccess(null);
    }

    /**
     * 构建失败信息
     * @param code  失败的状态码
     * @param msg   失败的提示信息
     * @return
     */
    public static Result buildFail(Integer code,String msg){
        Result result=new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }


    /**
     * 构建失败
     * @param resultEnum 传入对应的枚举类
     * @return
     */
    public static  Result buildFail(ResultEnum resultEnum){
        Result result=new Result();
        result.setCode(resultEnum.getCode());
        result.setMsg(resultEnum.getMessage());
        return result;
    }

    public static Result<Map<String, String>> buildFieldError(Map<String, String> errors) {
        Result<Map<String, String>> result = new Result<>();
        result.setCode(-1); // 或 ResultEnum.PARAM_ERROR.getCode()
        result.setMsg("参数校验失败");
        result.setData(errors); // 存放 {字段名: 错误信息} 映射
        return result;
    }

    public static  Result buildFail(String msg){
        Result result = new Result<>();
        result.setCode(5000);
        result.setMsg(msg);
        return result;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
